package com.xd.cloud.web;


import com.xd.cloud.common.bean.MessageBean;
import com.xd.cloud.dao.Page;
import com.xd.cloud.entity.core.Menu;
import com.xd.cloud.service.core.MenueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangtao
 * @class_name MenuController
 * @description menu请求处理层
 * @create 2017/12/19
 */
@Controller
public class MenuController {

    @Autowired
    private MenueService menueService;

    /**
     * 获取所有菜单列表
     * @return Object
     */
    @RequestMapping(value = "/menu/",method = RequestMethod.GET,headers = "Accept=application/json")
    @ResponseBody
    public Object getMenuList(HttpServletRequest request, HttpServletResponse response) {
        //直接返回前台
        String draw = request.getParameter("draw");
        //数据起始位置
//        String startIndex = request.getParameter("start");
        //每页显示的条数
        String num = request.getParameter("page");
        String pageSize = request.getParameter("limit");
        int sindex = Integer.parseInt(num);
        int ps = Integer.parseInt(pageSize);
        Page page = menueService.getAllMenu(sindex, ps);
        Map<Object, Object> info = new HashMap<Object, Object>();
        info.put("pageData", page.getResult());
        info.put("total", page.getTotalCount());
        info.put("draw", draw);
        return info;
    }

    /**
     * 添加父类菜单
     * @return Object
     */
    @RequestMapping(value = "/menu",method = RequestMethod.POST,headers = "Accept=application/json")
    @ResponseBody
    public Object saveMenu(HttpServletRequest request, HttpServletResponse response, @RequestBody Menu menu) {
        List<Menu> _menu = menueService.findMenuByModule(menu.getModule());
        if(_menu.size()>0){
            return new MessageBean(false,"该菜单配置已经存在啊");
        }
        menu.setPorder(10000);
        Menu saveMenu =  menueService.saveMenu(menu);
        return new MessageBean(true,saveMenu);
    }

    /**
     * 编辑菜单
     * @return Object
     */
    @RequestMapping(value = "/menu/{menuid}",method = RequestMethod.PUT,headers = "Accept=application/json")
    @ResponseBody
    public Object editMenu(HttpServletRequest request, HttpServletResponse response,@PathVariable Integer menuid, @RequestBody Menu menu) {
        Menu oldMenu = menueService.getMenueById(menuid);

        oldMenu.setName(menu.getName());
        oldMenu.setModule(menu.getModule());
        oldMenu.setSubModule(menu.getSubModule());
        menueService.updateMenu(oldMenu);
        return new MessageBean(true,oldMenu);
    }

    /**
     * 删除菜单
     * @return Object
     */
    @RequestMapping(value = "/menu/{menuid}",method = RequestMethod.DELETE,headers = "Accept=application/json")
    @ResponseBody
    public Object delMenuByid(HttpServletRequest request, HttpServletResponse response,@PathVariable Integer menuid) {
        Menu menu = menueService.getMenueById(menuid);
        try{
            menueService.delMenu(menu);
        }catch (Exception e){
            return new MessageBean(false,"请先删除子菜单");
        }
        return new MessageBean(true);
    }


    /**
     * 添加子菜单
     * @return Object
     */
    @RequestMapping(value = "/parent/{pid}/menu/",method = RequestMethod.POST,headers = "Accept=application/json")
    @ResponseBody
    public Object addChildMenu(HttpServletRequest request, HttpServletResponse response,@PathVariable Integer pid,@RequestBody Menu menu) {
        List<Menu> _menu = menueService.findMenuBySubModule(menu.getSubModule());
        if(_menu.size()>0){
            return new MessageBean(false,"该菜单配置已经存在啊");
        }
        Menu parentMenu = menueService.getMenueById(pid);
        menu.setParentMenu(parentMenu);
        menu.setCorder(10000);
        menueService.saveMenu(menu);
        return new MessageBean(true,menu);
    }


    @RequestMapping(value = "/menu/tree",method = RequestMethod.GET,headers = "Accept=application/json")
    @ResponseBody
    public Object getMenuTree(HttpServletRequest request, HttpServletResponse response){
        List list = menueService.getMenuTree();
        return list;
    }


    @RequestMapping(value = "/parentid/{parentid}/menu/",method = RequestMethod.GET,headers = "Accept=application/json")
    @ResponseBody
    public Object getChildMenuByPid(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer parentid){
        List<Menu> list = menueService.getMenuesByPId(parentid);
        return list;
    }

    @RequestMapping(value = "/order/menu/",method = RequestMethod.PATCH,headers = "Accept=application/json")
    @ResponseBody
    public Object orderMenuByPids(HttpServletRequest request, HttpServletResponse response, @RequestBody Integer ids[]){
        menueService.orderMenuByPids(ids);
        return ids;
    }

    @RequestMapping(value = "/order/parent/{parentid}/menu/",method = RequestMethod.PATCH,headers = "Accept=application/json")
    @ResponseBody
    public Object orderchildMenuByids(HttpServletRequest request, HttpServletResponse response,@PathVariable Integer parentid, @RequestBody Integer ids[]){
        menueService.orderchildMenuByids(ids);
        return ids;
    }
}
